<?php
/***************************************************************
*  Copyright notice
*  (c) 2009, jianyuzhu@gmail.com
*  All rights reserved
*  This script is part of the PPEMI project.
***************************************************************/

//
include_once(CONFIG_PATH . CONFIG_DIR_CLASSES . 'base.item.user.class.php');

class UserLogBase extends BaseItemUserBase {
	var $table = 'pp_users_logs';
	var $prefix = 'logs_';
	//
	var $sort_order = true;
	var $sort_fields = array('id-');
	
	//constructor
	function UserLogBase() {
		parent::BaseItemUserBase();
		
		$this->owner = $this->_get_current_user();
		$this->session = $this->_get_session_id();
	}
	
	//methods
	function addLog($action_name, $action_id = '') {
		$data_array = array(
					'logs_ip_address' => util_get_ip_address(),
					'logs_action_name' => $action_name,
					'logs_action_id' => $action_id,
					'logs_action_time' => time(),
					'logs_request_url' => SYS_RT_REQUEST_URI,
					'logs_referer_url' => $_SERVER['HTTP_REFERER'],
					'logs_session' => $this->session,
						);
		
		return $this->addLogA($data_array);
	}
	
	function addLogA($data_array) {
		$data_array = func_db_input($data_array);
		
		$data_array['users_id'] = $this->owner;
		$data_array['logs_date_added'] = 'now()';
		$data_array['logs_added_by'] = $this->_get_current_user();
		$data_array['logs_status'] = '1';
		
		return $this->_insert('pp_users_logs', $data_array);
	}
	
	function getLog($logs_id) {
		$value = func_db_input($value);
		
		$query = "select * from pp_users_logs where users_id = '" . $this->owner . "' and logs_id = '" . (int)$logs_id . "'";
		
		return $this->adb->getRow($query);
	}
	
	function getLogs($filter = '', $check = 0, $limit = true) {
		$query_limit_str = "";
		if( $limit == true ) {
			$query_limit_str = $this->getQueryLimitStr();
		}
		$query_str = "";
		if( is_array($filter) ) {
			$filter = func_db_input($filter);
		}
		$query = "select * from pp_users_logs where users_id = '" . $this->owner . "' " . $query_str . $query_limit_str;
		
		return $this->adb->getRows($query);
	}
	
	function getLogsTotal($filter = '', $check = 0) {
		$query_str = "";
		if( is_array($filter) ) {
			$filter = func_db_input($filter);
		}
		$query = "select count(*) as count from pp_users_logs where users_id = '" . $this->owner . "' " . $query_str;
		
		return $this->adb->getCount($query);
	}
	
	function updateLog($logs_id, $data_array) {
		$data_array = func_db_input($data_array);
		
		$data_array['logs_last_modified'] = 'now()';
		$data_array['logs_modified_by'] = $this->_get_current_user();
		
		return $this->_update('pp_users_logs', $data_array, "users_id = '" . $this->owner . "' and logs_id = '" . (int)$logs_id . "'");
	}
	
	function deleteLog($logs_id) {
		$query = "delete from pp_users_logs where users_id = '" . $this->owner . "' and logs_id = '" . (int)$logs_id . "'";
		return $this->adb->query($query);
	}
	
	//methods
	function clean() {
		$query = "delete from pp_users_logs where users_id = '" . $this->owner . "'";
		return $this->adb->query($query);
	}
}
//
?>